package com.enqualcomm.kids.config.userdefault;

import com.enqualcomm.kids.component.MyApplication;
import com.enqualcomm.kids.network.socket.response.QueryUserTerminalInfoResult;
import common.utils.BufferedSharedPreferences;
import common.utils.GsonFactory;

/**
 * Created by yinzhiqun on 2015/10/14.
 *
 */
public class UserTerminalDefault {
    private final BufferedSharedPreferences sp;
    private final String userterminalid;

    public UserTerminalDefault(String userterminalid) {
        this.userterminalid = userterminalid;
        sp = MyApplication.getInstance().getSp(userterminalid+"_ut");
    }

    public String getUserterminalid() {
        return userterminalid;
    }


    public void setInfo(QueryUserTerminalInfoResult.Data info) {
        CacheEntry.set(Keys.UserTerminal.INFO+userterminalid,info);//同步修改缓存

        if (info!=null){
            String s = GsonFactory.newInstance().toJson(info);
            sp.saveString(Keys.UserTerminal.INFO,s);
        }else{
            sp.remove(Keys.UserTerminal.INFO);
        }
    }

    public QueryUserTerminalInfoResult.Data getInfo(){
        QueryUserTerminalInfoResult.Data obj = (QueryUserTerminalInfoResult.Data) CacheEntry.get(Keys.UserTerminal.INFO+userterminalid);

        if (obj!=null){//如果有缓存，直接从缓存里面取
            return obj;
        }else{//如果没有，从sp里面取
            String s = sp.getString(Keys.UserTerminal.INFO, null);
            if (s!=null){
                obj = GsonFactory.newInstance().fromJson(s, QueryUserTerminalInfoResult.Data.class);
                CacheEntry.set(Keys.UserTerminal.INFO+userterminalid,obj);
                return obj;
            }
            return null;
        }
    }

    /**
     * 获取用户头像(老版本用户设置的头像)
     * @param headerPath
     */
    public void setHeader(String headerPath){
        CacheEntry.set(Keys.UserTerminal.HEADER+userterminalid,headerPath);

        if (headerPath!=null){
            sp.saveString(Keys.UserTerminal.HEADER,headerPath);
        }else{
            sp.remove(Keys.UserTerminal.HEADER);
        }
    }

    public String getHeader(){
        String str = (String)CacheEntry.get(Keys.UserTerminal.HEADER+userterminalid);
        if (str==null){
            str = sp.getString(Keys.UserTerminal.HEADER, "relationship_9.png");
            CacheEntry.set(Keys.UserTerminal.HEADER+userterminalid,str);
        }
        return str;
    }

    /**
     * 获取手表头像
     * @return
     */
    public String getIconPath(){
        String str = (String)CacheEntry.get(Keys.UserTerminal.ICON+userterminalid);
        if (str==null){
            str = sp.getString(Keys.UserTerminal.ICON, null);
            CacheEntry.set(Keys.UserTerminal.ICON+userterminalid, str);
        }
        return str;
    }

    public void setIconPath(String path){
        CacheEntry.set(Keys.UserTerminal.ICON+userterminalid,path);

        if (path!=null){
            sp.saveString(Keys.UserTerminal.ICON,path);
        }else{
            sp.remove(Keys.UserTerminal.ICON);
        }
    }


}
